package com.eclab.hello.controller;

import com.eclab.hello.enums.ResultEnum;
import com.eclab.hello.exception.UserException;
import com.eclab.hello.imodelview.UserDetailView;
import com.eclab.hello.model.BO.RestResponseBo;
import com.eclab.hello.model.User;
import com.eclab.hello.model.VO.UserOutputVO;
import com.eclab.hello.service.UserService;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 描述:
 * 用户控制器
 * @author yd
 * @create 2019-05-28 15:14
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(value = "/user", description = "用户控制器")
public class UserController extends BaseController{

    @Autowired
    private UserService userService;

    /**
     * 获取用户列表
     * @return
     */
    @GetMapping
    @ApiOperation(value = "")
    public RestResponseBo query(@RequestParam (value = "page",defaultValue = "10") @ApiParam(name = "pageSize",value = "每页大小") Integer pageSize,
                                @RequestParam (value = "page",defaultValue = "1") @ApiParam(name = "pageNo",value = "当前页数") Integer pageNo){
        Pageable pageable = wrapPageable();
        Page<UserOutputVO> userPage = userService.listByPage(pageable);
        log.info("【userPage】:{}", userPage.getContent());
        return RestResponseBo.ok(userPage.getContent(), userPage);
    }

    /**
     * 根据id查询用户详情
     * @param id
     * @return
     */
    @JsonView(UserDetailView.class)
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据id查询用户详情")
    public RestResponseBo getInfo(@PathVariable @ApiParam(required = true) Integer id){
        User user = userService.getUser(id);
        if (true){
            throw new UserException(ResultEnum.USER_NOT_FOUND);
        }
        return RestResponseBo.ok(user, 200);
    }
    /**
     * 创建一个新用户
     * @param user
     * @return
     */
    @PostMapping
    @ApiOperation(value = "创建一个新用户")
    public RestResponseBo create(@Valid @RequestBody @ApiParam(required = true) User user,
                       BindingResult bindingResult){


        if (bindingResult.hasErrors()){
            bindingResult.getAllErrors().stream().forEach(error -> {
                log.info("【校验错误信息】：{}", error.getDefaultMessage());
            });
            throw new UserException(ResultEnum.PARAM_ERROR);
        }

        userService.create(user);
        return RestResponseBo.ok(user);

    }

    /**
     * 更新一个用户
     * @param user
     * @return
     */
    @PutMapping
    @ApiOperation(value = "更新一个用户")
    public RestResponseBo update(@Valid @RequestBody @ApiParam(required = true) User user,
                       BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            bindingResult.getAllErrors().stream().forEach(error -> {
                log.info("【校验错误信息】：{}", error.getDefaultMessage());
            });
            throw new UserException(ResultEnum.PARAM_ERROR);
        }

        userService.update(user);
        return RestResponseBo.ok(user);

    }

    /**
     * 删除用户信息
     * @param id
     */
    @DeleteMapping("/{id:\\d+}")
    @ApiOperation(value = "删除用户信息")
    public RestResponseBo delete(@PathVariable @ApiParam(required = true) Integer id){

        userService.delete(id);

        log.info("【删除用户】：{}，成功", id);
        return RestResponseBo.ok();
    }

    /**
     * 模拟自定义参数绑定
     * @param id
     * @param date
     * @return
     */
    @ApiOperation(value = "模拟自定义参数绑定")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public RestResponseBo detail(@RequestParam("id") long id, Date date) {
        log.info("data:{}", date);
        return RestResponseBo.ok(date);
    }

    /*@GetMapping(value = "/detail")
    public ModelAndView detail(@RequestParam("id") Integer id,
                               @ModelAttribute("message") String message) {
        System.out.println(message);
        ModelAndView view = new ModelAndView("user");
        User user = userService.getUser(id);
        view.addObject("user", user);
        return view;
    }*/

}






